import request from "@/utils/request";
import { parseStrEmpty } from "@/utils/common";

const user = "user";
const user_profile = "user/profile";

// 获取用户详细信息
export function getUserDetail(userId) {
  return request({
    url: `${user}/${parseStrEmpty(userId)}`,
    method: `get`,
  });
}

// 查询用户列表
export function listUser(query) {
  return request({
    url: `${user}/list`,
    method: `get`,
    params: query,
  });
}

// 新增用户
export function addUser(data) {
  return request({
    url: `${user}`,
    method: `post`,
    data,
  });
}

// 修改用户
export function updateUser(data) {
  return request({
    url: `${user}`,
    method: `put`,
    data,
  });
}

// 用户状态修改
export function changeUserStatus(userId, status) {
  const data = {
    userId,
    status,
  };
  return request({
    url: `${user}/status`,
    method: `put`,
    data,
  });
}

// 用户密码修改
export function resetUserPassword(userId, newPassword) {
  const data = {
    userId,
    newPassword,
  };
  return request({
    url: `${user}/password`,
    method: `put`,
    data,
  });
}

// 删除用户
export function deleteUser(userIds) {
  return request({
    url: `${user}/${userIds}`,
    method: `delete`,
  });
}

// 查询用户个人信息
export function getUserProfile() {
  return request({
    url: `${user_profile}`,
    method: `get`,
  });
}

// 修改用户个人信息
export function updateUserProfile(data) {
  return request({
    url: `${user_profile}`,
    method: `put`,
    data,
  });
}

// 修改用户密码
export function updateUserPassword(oldPassword, newPassword) {
  const data = {
    oldPassword,
    newPassword,
  };
  return request({
    url: `${user}/password`,
    method: `put`,
    data,
  });
}

// 用户头像上传
export function uploadAvatar(data) {
  return request({
    url: `${user_profile}/avatar`,
    method: `post`,
    data,
  });
}

// 查询授权角色
export function getRoleOfUser(userId) {
  return request({
    url: `${user_profile}/${userId}/role/`,
    method: `get`,
  });
}

// 保存授权角色
export function updateRoleOfUser(data) {
  const { userId } = data.userId;
  return request({
    url: `${user_profile}/${userId}/role/`,
    method: `put`,
    params: data,
  });
}
